/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package me.aviator.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import me.aviator.dao.CommonDao;
import me.aviator.entity.MBasemap;
import org.apache.commons.io.FileUtils;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class BasemapService {
    private final CommonDao commonDao;
    public BasemapService(Session session){
        commonDao=new CommonDao(session);
    }
    public List<MBasemap> listAll(){
        return commonDao.find(MBasemap.class);
    }
    
    public List<MBasemap> listByCatagory(String groupName){
        Criterion criterion=Restrictions.eq("catagory", groupName);
        return commonDao.find(MBasemap.class,criterion);
    }
    
//    public List<MBasemap> listByCatagoryB(String groupName){
//        Criterion criterion=Restrictions.eq("catagoryB", groupName);
//        return commonDao.find(MBasemap.class,criterion);
//    }
    
    public MBasemap findSvgMapById(int id){
		
        Criterion criterion = Restrictions.eq("id", id);
		
        List<MBasemap> maplist = commonDao.find(MBasemap.class, criterion);
		
        if(maplist.isEmpty()){
            return null;
        }
		
        return maplist.get(0);
    }
    
    public void uploadBasemap(String svgName,String catagory,String project,String scale,String administration,String scope,File svgxml,File hdpiThumbnail){
        MBasemap basemap=new MBasemap();
        basemap.setName(svgName);
        basemap.setCatagory(catagory);
        basemap.setProject(project);
        basemap.setScale(scale);
        basemap.setAdministration(administration);
        basemap.setScope(scope);
        String svgXml =null;
	try {
                svgXml = FileUtils.readFileToString(svgxml);
		basemap.setSvgxml(svgXml.getBytes("UTF-8"));
			
		basemap.setHdpithumbnail(FileUtils.readFileToByteArray(hdpiThumbnail));
			
	} catch (IOException e) {
		e.printStackTrace();
	}
        commonDao.save(basemap);
    }
    
    public void addBasemap(MBasemap basemap){
        commonDao.save(basemap);
    }
    
    public void removeBasemap(MBasemap basemap){
        commonDao.delete(basemap);
    }
    
    public List<MBasemap> findSvgMapByNamed(String SvgMapName){
	Criterion criterion = Restrictions.eq("name", SvgMapName);
		
	List<MBasemap> maplist = commonDao.find(MBasemap.class, criterion);
		
	if(maplist.isEmpty()){
            return null;
	}
		
	return maplist;	
    }
    
    public List<MBasemap> findSvgMap(Map<String,?> conditions){
        Criterion criterion=Restrictions.allEq(conditions);
        List<MBasemap> maplist = commonDao.find(MBasemap.class, criterion);
		
	if(maplist.isEmpty()){
            return null;
	}
		
	return maplist;	
    }
    
}
